package com.zln.hello;

//public class LinkedListStack {
//}

import java.util.EmptyStackException;

public class LinkedListStack {
    private ListNode stackPeek; // 栈顶元素
    private int stkSize = 0;    // 栈的大小

    // 链表节点
    private static class ListNode {
        int val;
        ListNode next;
        ListNode(int val) { this.val = val; }
    }

    // 构造函数
    public LinkedListStack() {
        stackPeek = null;
    }

    // 获取栈的大小
    public int size() {
        return stkSize;
    }

    // 判断栈是否为空
    public boolean isEmpty() {
        return stkSize == 0;
    }

    // 入栈
    public void push(int num) {
        ListNode node = new ListNode(num);
        node.next = stackPeek;
        stackPeek = node;
        stkSize++;
    }

    // 出栈
    public int pop() {
        if (isEmpty())
            throw new EmptyStackException();
        int num = peek();
        stackPeek = stackPeek.next;
        stkSize--;
        return num;
    }

    // 访问栈顶元素
    public int peek() {
        if (isEmpty())
            throw new EmptyStackException();
        return stackPeek.val;
    }

    // 将栈转换为数组
    public int[] toArray() {
        ListNode node = stackPeek;
        int[] res = new int[size()];
        for (int i = res.length - 1; i >= 0; i--) {
            res[i] = node.val;
            node = node.next;
        }
        return res;
    }

    // 测试栈的功能
    public static void main(String[] args) {
        LinkedListStack stack = new LinkedListStack();

        System.out.println("Is stack empty? " + stack.isEmpty()); // 检查栈是否为空

        // 入栈操作
        stack.push(10);
        stack.push(20);
        stack.push(30);

        System.out.println("Current stack size: " + stack.size()); // 输出栈的大小
        System.out.println("Peek top element: " + stack.peek());  // 查看栈顶元素

        // 打印栈内容
        System.out.println("Stack contents as array: ");
        int[] stackArray = stack.toArray();
        for (int i : stackArray) {
            System.out.print(i + " ");
        }
        System.out.println(); // 换行

        // 出栈操作
        System.out.println("Pop element: " + stack.pop()); // 出栈
        System.out.println("Current stack size after pop: " + stack.size()); // 栈的大小
        System.out.println("Peek top element after pop: " + stack.peek()); // 查看栈顶元素

        // 打印栈内容
        System.out.println("Stack contents after pop:");
        stackArray = stack.toArray();
        for (int i : stackArray) {
            System.out.print(i + " ");
        }
        System.out.println(); // 换行

        // 出栈剩下的元素
        System.out.println("Pop element: " + stack.pop());
        System.out.println("Pop element: " + stack.pop());

        // 栈为空时，调用 peek 或 pop 应该抛出异常
        try {
            System.out.println("Peek top element on empty stack: " + stack.peek());
        } catch (EmptyStackException e) {
            System.out.println("Caught exception: " + e);
        }

        try {
            System.out.println("Pop element on empty stack: " + stack.pop());
        } catch (EmptyStackException e) {
            System.out.println("Caught exception: " + e);
        }

        System.out.println("Is stack empty? " + stack.isEmpty()); // 栈为空
    }
}

